<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-left">
        <div class="system-intro">
          <h2 class="system-title">数据资源管理系统</h2>
          <div class="system-features">
            <div class="feature-item">
              <svg-icon icon-class="chart" class="feature-icon" />
              <span>智能数据分析</span>
            </div>
            <div class="feature-item">
              <svg-icon icon-class="message" class="feature-icon" />
              <span>评论情感分析</span>
            </div>
            <div class="feature-item">
              <svg-icon icon-class="trend" class="feature-icon" />
              <span>趋势可视化</span>
            </div>
            <div class="feature-item">
              <svg-icon icon-class="security" class="feature-icon" />
              <span>数据安全保障</span>
            </div>
          </div>
          <p class="system-desc">
            专业的数据资源管理的平台，助力数据有效管理，提高数据质量与价值
          </p>
        </div>
      </div>
      <div class="login-right">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <div class="logo-area">
            <svg-icon icon-class="logo" class="logo-icon" />
            <h3 class="login-title">管理员登录</h3>
          </div>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="用户名"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- 底部 -->
    <div class="el-login-footer">
      <span>Copyright © 2025 数据资源管理系统 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: "电商平台评论分析系统",
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$primary-color: #367fa9;
$secondary-color: #4ecdc4;
$accent-color: #f08a5d;
$text-primary: #2c3e50;
$text-secondary: #7f8c8d;
$bg-gradient-start: #367fa9;
$bg-gradient-end: #2980b9;

.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, $bg-gradient-start, $bg-gradient-end);
  position: relative;
  overflow: hidden;
  
  // 装饰性元素
  &::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255,255,255,0.05) 1px, transparent 1px);
    background-size: 20px 20px;
    animation: float 15s ease-in-out infinite;
  }
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0) rotate(0deg);
  }
  50% {
    transform: translate(-20px, -20px) rotate(2deg);
  }
}

.login-wrapper {
  display: flex;
  max-width: 1000px;
  width: 90%;
  height: 600px;
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  z-index: 1;
  
  @media (max-width: 768px) {
    flex-direction: column;
    height: auto;
    width: 95%;
  }
}

.login-left {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
  background: linear-gradient(135deg, $primary-color, $bg-gradient-end);
  color: #ffffff;
  
  @media (max-width: 768px) {
    padding: 30px 20px;
    order: 2;
  }
}

.system-title {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 30px;
  text-align: center;
  letter-spacing: 1px;
  
  @media (max-width: 768px) {
    font-size: 24px;
  }
}

.system-features {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 30px;
  
  @media (max-width: 768px) {
    grid-template-columns: 1fr;
    gap: 15px;
  }
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 15px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  transition: transform 0.3s ease, background 0.3s ease;
  
  &:hover {
    transform: translateY(-3px);
    background: rgba(255, 255, 255, 0.15);
  }
}

.feature-icon {
  font-size: 20px;
  color: $secondary-color;
}

.system-desc {
  font-size: 16px;
  line-height: 1.6;
  text-align: center;
  opacity: 0.9;
  
  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.login-right {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  
  @media (max-width: 768px) {
    padding: 30px 20px;
    order: 1;
  }
}

.login-form {
  width: 100%;
  max-width: 350px;
}

.logo-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
}

.logo-icon {
  font-size: 48px;
  color: $primary-color;
  margin-bottom: 15px;
  
  @media (max-width: 768px) {
    font-size: 36px;
  }
}

.login-title {
  font-size: 22px;
  font-weight: 600;
  color: $text-primary;
  margin: 0;
}

.el-input {
  height: 44px;
  margin-bottom: 15px;
  
  input {
    height: 44px;
    border-radius: 6px;
    border: 1px solid #e0e0e0;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
    
    &:focus {
      border-color: $primary-color;
      box-shadow: 0 0 0 2px rgba(54, 127, 169, 0.2);
    }
  }
}

.input-icon {
  height: 45px;
  width: 16px;
  color: $text-secondary;
}

.login-code {
  width: 33%;
  height: 44px;
  float: right;
  
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 6px;
  }
}

.login-code-img {
  height: 44px;
  width: 100%;
  object-fit: cover;
}

.el-checkbox {
  margin: 15px 0;
  
  .el-checkbox__label {
    color: $text-secondary;
  }
}

.el-button {
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  background: $primary-color;
  border: none;
  border-radius: 6px;
  transition: all 0.3s ease;
  
  &:hover {
    background: darken($primary-color, 5%);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(54, 127, 169, 0.3);
  }
  
  &:active {
    transform: translateY(0);
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: absolute;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 1;
}

.link-type {
  color: $primary-color;
  transition: color 0.3s ease;
  
  &:hover {
    color: darken($primary-color, 10%);
  }
}
</style>
